<template>
    <div class="app-container">
        <el-form ref="queryForm" :model="queryParams" :inline="true" label-width="68px">
            <el-form-item label="昵称" prop="nickname">
                <el-input v-model="queryParams.nickname" placeholder="请输入用户昵称" clearable size="small"
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="手机" prop="phone">
                <el-input v-model="queryParams.phone" placeholder="请输入用户手机" clearable size="small"
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="性别" prop="sex">
                <el-select v-model="queryParams.sex" clearable placeholder="请选择性别" size="small">
                    <el-option v-for="item in sexOptions" :key="item.dictValue" :label="item.dictLabel"
                        :value="item.dictValue.toString()"></el-option>
                </el-select>
            </el-form-item>

            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>



        <el-table v-loading="loading" :data="generaluserList">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="头像" align="center" prop="avatar" :show-overflow-tooltip="true">
                <template slot-scope="scope">
                    <el-avatar :size="50" :src="scope.row.avatar"></el-avatar>
                </template>
            </el-table-column>
            <el-table-column label="昵称" align="center" prop="nickname" :show-overflow-tooltip="true" />
            <el-table-column label="手机" align="center" prop="phone" width="120" :show-overflow-tooltip="true" />
           <el-table-column label="积分" align="center" prop="integral" :show-overflow-tooltip="true" />
        </el-table>

        <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageIndex"
            :limit.sync="queryParams.pageSize" @pagination="getList" />

        <!-- 添加或修改参数配置对话框 -->

        <el-dialog :title="title" :visible.sync="open" width="500px">
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="用户头像" prop="avatar">
                    <el-avatar :size="80" :src="form.avatar"></el-avatar>
                </el-form-item>

                <el-form-item label="用户昵称" prop="nickname">
                    <el-input v-model="form.nickname" placeholder="用户昵称" />
                </el-form-item>
                <el-form-item label="手机号码" prop="phone">
                    <el-input v-model="form.phone" placeholder="手机号码" />
                </el-form-item>
                <el-form-item label="剩余积分" prop="integral">
                    <el-input-number v-model="form.integral" :precision="2" :step="0.1" :min="0" label="用户剩余积分"
                        :disabled="true">
                    </el-input-number>
                </el-form-item>
                <!--                <el-form-item label="可邀请数" prop="invitationCount">-->
                <!--                    <el-input-number v-model="form.invitationCount" :min="0" label="剩余可邀请数"></el-input-number>-->
                <!--                </el-form-item>-->

                <el-form-item label="性别" prop="sex">
                    <el-radio-group v-model="form.sex">
                        <el-radio v-for="(item,index) in sexOptions " :key="index" :label="Number(item.dictValue)">
                            {{ item.dictLabel }}
                        </el-radio>
                    </el-radio-group>
                </el-form-item>

                <el-form-item label="状态" prop="status">
                    <el-switch v-model="form.status" active-value="T" inactive-value="F"></el-switch>
                </el-form-item>
                <!--                <el-form-item label="禁言" prop="isBanned">-->
                <!--                    <el-switch v-model="form.isBanned" active-value="T" inactive-value="F"></el-switch>-->
                <!--                </el-form-item>-->
                <el-form-item label="用户备注" prop="mark">
                    <el-input ype="textarea" v-model="form.mark" placeholder="用户备注" maxlength="200" show-word-limit
                        :rows="4" />
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>


    </div>
</template>

<script>
import {
    listgeneralUser,
    addgeneralUser,
    updategeneralUser,
} from "@/api/generaluser/userlist";


export default {
    name: "Config",
    props: {
        userId: {
            type: Number,
            default: () => {
                return 0;
            }
        },
    },
    data () {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 总条数
            total: 0,
            // 参数表格数据
            configList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 类型数据字典
            typeOptions: [],
            // 日期范围
            dateRange: [],
            // 查询参数
            queryParams: {
                pageIndex: 1,
                pageSize: 10,
                userId: this.userId,
                type: 1,
            },
            // 表单参数
            form: {
                userId: this.userId
            },
            // 表单校验
            rules: {
                avatar: [{ required: true, message: "头像不能为空", trigger: "blur" }],
                nickname: [
                    {
                        required: true,
                        message: "用户昵称不能为空",
                        trigger: "blur",
                    },
                ],
                // phone: [
                //     {required: true, message: "手机号码不能为空", trigger: "blur"},
                // ],
            },
            generaluserList: [], //列表
            sexOptions: [], //性别选项
            relationshipOptions: [], //家庭身份
            usertypeOptions: [], //用户类型
            boolOptions: [], //是否
            userLevelOptions: [], //会员级别

            userTypeOptions: [], //会员类型：普通会员；官方会员
            channelOptions: [], //会员来源渠道

            //vip 充值记录组件
            openVIP: false,
            userId: undefined,
            //家庭成员
            open_FamilyMember: false,
            familyCode: undefined,
            //家庭孩子
            open_FamilyChild: false,
            // familyCode: undefined,
            // 发放记录列表弹出层标题
            title_user_coupon: "",
            // 是否显示发放记录列表弹出层
            open_user_coupon: false,
            phone: undefined,

        };
    },
    created () {
        this.getList();
        this.sexOptions = this.$store.getters.parameter.sys_user_sex;
        this.relationshipOptions = this.$store.getters.parameter.sys_family_relationship;
        this.usertypeOptions = this.$store.getters.parameter.general_user_type;
        this.boolOptions = this.$store.getters.parameter.sys_bool;
        this.userLevelOptions = this.$store.getters.parameter.general_user_level;

        this.userTypeOptions = this.$store.getters.parameter.general_user_type;
        this.channelOptions = this.$store.getters.parameter.general_user_channel;
        // general_user_type
    },
    watch: {
        //监听子控件内容的改动,发送给父级组件
        userId: {
            handler (newValue, oldValue) {
                this.queryParams.userId = this.userId;
                this.queryParams.phone = this.phone
                this.getList();
            },
            deep: true
        },
        phone: {
            handler (newValue, oldValue) {
                this.queryParams.userId = this.userId;
                this.queryParams.phone = this.phone
                this.getList();
            },
            deep: true,
        },
    },
    methods: {
        /** 查询参数列表 */
        getList () {
            this.loading = true;
            listgeneralUser(this.addDateRange(this.queryParams, this.dateRange)).then(
                (response) => {
                    // console.log(response)
                    this.generaluserList = response.data.list;
                    this.total = response.data.count;
                    this.loading = false;
                }
            );
        },
        // 取消按钮
        cancel () {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset () {
            this.form = {
                avatar: undefined,
                familyCode: undefined,
                familyRelationship: undefined,
                groupId: undefined,
                integral: undefined,
                invitationCount: undefined,
                isBanned: undefined,
                isFamilyOwner: undefined,
                isPromoter: undefined,
                lastIp: undefined,
                lastTime: undefined,
                level: undefined,
                loginType: undefined,
                mark: undefined,
                nickname: undefined,
                payCount: undefined,
                phone: undefined,
                sex: undefined,
                signNum: undefined,
                spreadCount: undefined,
                spreadTime: undefined,
                spreadUid: undefined,
                status: undefined,
                userId: this.userId,
                userType: undefined,

            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery () {
            this.queryParams.pageIndex = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery () {
            this.dateRange = [];
            this.resetForm("queryForm");
            this.handleQuery();
        },


        /** 提交按钮 */
        submitForm: function () {
            this.$refs["form"].validate((valid) => {
                if (valid) {
                    if (this.form.userId !== undefined) {
                        updategeneralUser(this.form).then((response) => {
                            if (response.code === 200) {
                                this.msgSuccess("修改成功");
                                this.open = false;
                                this.getList();
                            } else {
                                this.msgError(response.msg);
                            }
                        });
                    } else {
                        addgeneralUser(this.form).then((response) => {
                            if (response.code === 200) {
                                this.msgSuccess("新增成功");
                                this.open = false;
                                this.getList();
                            } else {
                                this.msgError(response.msg);
                            }
                        });
                    }
                }
            });
        },


    },
};
</script>
